﻿using System;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

namespace JQMergeFramework
{
    public class UICityLvTips : MonoBehaviour
    {
        public static UICityLvTips I;
        public Transform uiTopLayer;
        public Transform uiBottomLayer;
        
        private RectTransform rect;
        
        private float moveY = 300;
        private void Awake()
        {
            I = this;

            rect = this.GetComponent<RectTransform>();
        }

        public void SetShow(bool isShow)
        {
            this.gameObject.SetActive(isShow);
        }

        public void ShowLvTips(CityUnit city)
        {
            
           GameObject obj = EffectPool.I.GetPoolObject(PoolName.CityLvText);
           Text textC = obj.GetComponent<Text>();
           textC.text = (city.Level + 1).ToString();
           textC.color = new Color32(253, 255, 60, 255);
           
           obj.transform.SetParent(this.uiBottomLayer);

           Vector3 pos = PositionConvert.WorldPointToScreenPoint(city.transform.position);
           Vector2 screenPoint =PositionConvert.ScreenPointToUIPoint(rect, pos);

           obj.transform.localPosition =   screenPoint;
           obj.transform.localScale = Vector3.one;
           DOTween.Kill(obj.GetInstanceID());
           
           Sequence sq = DOTween.Sequence().SetId(obj.GetInstanceID());
           sq.Append(obj.transform.DOLocalMoveY(obj.transform.localPosition.y+moveY, 1).SetEase(Ease.InOutQuad));
           sq.Join(textC.DOColor(new Color32(253, 255, 60,200),1f).SetEase(Ease.InOutQuad));
           sq.OnComplete(() =>
           {
               EffectPool.I.PutPoolObject(PoolName.CityLvText,obj);
           });
           sq.Play();
        }
        
        
        public void ShowLvTips(CityUnit city,int score)
        {
            GameObject obj = EffectPool.I.GetPoolObject(PoolName.CityLvText);
            Text textC = obj.GetComponent<Text>();
            textC.text = score.ToString();
            textC.color = new Color32(253, 255, 60, 255);
           
            obj.transform.SetParent(this.uiBottomLayer);

            Vector3 pos = PositionConvert.WorldPointToScreenPoint(city.transform.position);
            Vector2 screenPoint =PositionConvert.ScreenPointToUIPoint(rect, pos);

            obj.transform.localPosition =   screenPoint;
            obj.transform.localScale = Vector3.one;
            DOTween.Kill(obj.GetInstanceID());
           
            Sequence sq = DOTween.Sequence().SetId(obj.GetInstanceID());
            sq.Append(obj.transform.DOLocalMoveY(obj.transform.localPosition.y+moveY, 1).SetEase(Ease.InOutQuad));
            sq.Join(textC.DOColor(new Color32(253, 255, 60,200),1f).SetEase(Ease.InOutQuad));
            sq.OnComplete(() =>
            {
                EffectPool.I.PutPoolObject(PoolName.CityLvText,obj);
            });
            sq.Play();
        }
        
        
        public void ShowFillScoreTips(int score)
        {
            GameObject obj = EffectPool.I.GetPoolObject(PoolName.CityLvText);
            Text textC = obj.GetComponent<Text>();
            textC.text = string.Format("+{0}", score.ToString());
            textC.color = new Color32(253, 255, 60, 255);
           
            obj.transform.SetParent(this.uiTopLayer);

            Vector3 pos = UIGame.I.GetScoreFillUIPoint();
            obj.transform.localPosition = pos;
            obj.transform.localScale = Vector3.one;
            
            DOTween.Kill(obj.GetInstanceID());
           
            Sequence sq = DOTween.Sequence().SetId(obj.GetInstanceID());
            sq.Append(obj.transform.DOLocalMoveY(pos.y+100, 1).SetEase(Ease.InOutQuad));
            sq.Join(textC.DOColor(new Color32(253, 255, 60,200),1).SetEase(Ease.InOutQuad));
            sq.OnComplete(() =>
            {
                EffectPool.I.PutPoolObject(PoolName.CityLvText,obj);
            });
            sq.Play();
        }
        
    }
}